import type { Viewer } from "cesium"
import type { MouseHandler } from "../CustomCesium/CustomCesium"
import { MeasuringType } from "@@/constants/other-keys"
import { defined } from "cesium"
import _ from "lodash"
import MeasureDistance from "./Distance"
import MeasurePosition from "./Position"

/**
 * description: 测量工具
 * fileName: MeasuringTools.ts
 * author: ssr
 * date: 2023-01-12 15:43:29
 * version: V1.0.0
 */
export default class MeasuringTools {
  private viewer: Viewer
  private mouseHandlerManager: MouseHandler
  private measurePosition: MeasurePosition | null
  private measureDistance: MeasureDistance | null

  constructor(viewer: Viewer, mouseHandlerManager: MouseHandler) {
    this.mouseHandlerManager = mouseHandlerManager
    this.viewer = this.init(viewer)
    this.measurePosition = null
    this.measureDistance = null
  }

  private init(viewer: Viewer) {
    if (!defined(viewer)) throw new Error("EventListener,无效的视图对象！")
    return viewer
  }

  /**
   * 测量
   * @param measuringType
   */
  measuring(measuringType: MeasuringType) {
    switch (measuringType) {
      case MeasuringType.POSITION:
        if (!this.measurePosition) {
          this.measurePosition = new MeasurePosition(
            this.viewer,
            this.mouseHandlerManager
          )
        }
        this.measurePosition.measuring()
        break
      case MeasuringType.DISTANCE:
        if (!this.measureDistance) {
          this.measureDistance = new MeasureDistance(
            this.viewer,
            this.mouseHandlerManager
          )
        }
        this.measureDistance.measuring()
        break
    }
  }

  /**
   * 销毁
   */
  destory() {
    this.measurePosition && this.measurePosition.clearAll()
    this.measureDistance && this.measureDistance.clearAll()
  }
}
